package distributedRE.model;

import java.util.Hashtable;

import database.mediator.MediatorInterface;

import reader.ContentSummaryReader;
import searchEngine.SearchEngine;

public class DataBase {

	private long Id;
	private String url;
	private long samplesize;
	private long wordsNumber;
	private long documentsNumber;
	private String csPath;
	private String indexPath;
	private String localPath;
	private ContentSummaryReader csr;
	private SearchEngine searchEngine;
	private Hashtable<String,Document> documents;
	private long maxretrievable;
	private long concurrentQueries;

	public DataBase(long dId, String url, long samplesize, long wordsNumber,
			long documentsNumber, String csPath, String indexPath,
			String localPath, long maxretrievable, long concurrentQueries) {
		
		this.Id = dId;
		this.url = url;
		this.samplesize = samplesize;
		this.wordsNumber = wordsNumber;
		this.documentsNumber = documentsNumber;
		this.csPath = csPath;
		this.indexPath = indexPath;
		this.localPath = localPath;
		documents = new Hashtable<String, Document>();
		this.maxretrievable = maxretrievable;
		this.concurrentQueries = concurrentQueries;
		
	}

	public String getUrl(){
		return url;
	}
	
	public long getWordNumber(){
		return wordsNumber;
	}
	
	public long getId() {
		return Id;
	}

	public long getSampleResultSize() {
		return samplesize;
	}

	public String getLocalPath() {
		return localPath;
	}

	public long getNumberOfDocuments() {
		return documentsNumber;
	}

	public long getFrequency(String term) {
		return getContentSummaryReader().getFrequency(term);
	}

	private ContentSummaryReader getContentSummaryReader() {
		
		if (csr == null){
			
			csr = new ContentSummaryReader(getContentSummaryPath());
			
		}

		return csr;
		
	}

	private String getContentSummaryPath() {
		return csPath;
	}

	public String getIndexPath() {
		return indexPath;
	}

	public void setSearchEngine(SearchEngine searchEngine) {
		this.searchEngine = searchEngine;	
	}

	public Document getDocument(String doc,MediatorInterface mI) {
		
		Document d = documents.get(doc);
		
		if (d==null){
			d = mI.getDocument(Id,doc);
			
			documents.put(doc, d);
		}
		
		return d;
		
	}

	public SearchEngine getSearcher() {
		return this.searchEngine;
	}

	public boolean equals(Object o){
		return (Id == ((DataBase)o).Id);
	}
	
	public long getMaxRetrievable(){
		return maxretrievable;
	}
	
	public long getConcurrentQueriesNumber(){
		return concurrentQueries;
	}
	
}
